home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / virtmem.exe / EMS.PAS < prev    next >
Pascal/Delphi Source File  |  1992-06-05  |  19KB  |  547 lines

  1. unit EMS;
  2. {$O+}
  3. {$F+}
  4.  
  5. { *************************************************************
  6.   * This unit provides an interface to the basic functions of *
  7.   * the LIM Expanded Memory Specification. Since it does not  *
  8.   * use any of the LIM EMS 4.0 function calls, you can also   *
  9.   * use it on systems with EMS versions less than 4.0         *
  10.   ************************************************************* }
  11.  
  12. { Written by:
  13.     Peter Immarco.
  14.     Thought Dynamics
  15.     Manhattan Beach, CA
  16.     Compuserve ID# 73770,123
  17.      *** Public Domain ***
  18.  
  19.   Used by permission of the author.
  20. }
  21.  
  22. {Revised and made into a unit by Wayne Knorr}
  23.  
  24. { This unit provides the following functions:
  25.   +------------------------------------------------------------+
  26.   | * Makes sure the LIM Expanded Memory Manager (EMM) has     |
  27.   |   been installed in memory                                 |
  28.   | * Displays the version number of the EMM present in memory |
  29.   | * Determines if there are enough pages (16k blocks) of     |
  30.   |   memory for our test program's usage. It then displays    |
  31.   |   the total number of EMS pages present in the system,     |
  32.   |   and how many are available for our usage                 |
  33.   | * Requests the desired number of pages from the EMM        |
  34.   | * Maps a logical page onto one of the physical pages given |
  35.   |   to us                                                    |
  36.   | * Displays the base address of our EMS memory page frame   |
  37.   | * Returns the EMS memory given to us back to the EMM, and  |
  38.   |   exits                                                    |
  39.   +------------------------------------------------------------|}
  40.  
  41.  
  42. { All the calls are structured to return the result or error
  43.   code of the Expanded Memory function performed as an integer.
  44.   If the error code is not zero, which means the call failed,
  45.   a simple error procedure is called.}
  46.  
  47. Interface
  48.  
  49. uses CRT,DOS;
  50.  
  51. Const
  52.  
  53.   SizeOPhysicalPage=16*1024;
  54.   MaxPhysicalPage  =35;                                                      {Expected max mappable physical EMS page}
  55.  
  56. Type
  57.  
  58.      PhysicalPageRec=
  59.         Record
  60.           PhysPageSegment:Word;
  61.           PhysPageNumber :Word;
  62.         End;
  63.      PhysicalPageArr=Array [0..MaxPhysicalPage] of PhysicalPageRec;
  64.  
  65. Var
  66.  
  67.      EMSUseful    :Boolean;                  {Flag: We can use EMS}
  68.      EMSPageBase  :Word;                     {Current EMS page base}
  69.      EMSHandl     :Word;                     {EMS page handle for deallocation.}
  70.      EMSPageAvail :Word;                     {Number of logical Pages.}
  71.      NumOPhysicalPage:Word;                  {Number of physical pages.}
  72.      PhysicalPage    :PhysicalPageArr;       {Array of all valid physical pages.}
  73.  
  74. Procedure EMSInit(OverRide:Boolean);
  75. Function EMS_Pages_Available
  76.     (Var Total_EMS_Pages,Pages_Available: Word): Word;
  77. Function Allocate_Expanded_Memory_Pages
  78.     (Pages_Needed: Word; Var Handle: Word   ): Word;
  79. Function Map_Expanded_Memory_Pages
  80.     (Handle,Logical_Page,Physical_Page: Word): Word;
  81. Function Get_Page_Frame_Base_Address
  82.     (Var Page_Frame_Address: Word): Word;
  83. Function Deallocate_Expanded_Memory_Pages
  84.     (Handle: Word): Word;
  85.  
  86. Implementation
  87.  
  88. Type
  89.   ST3  = string[3];
  90.   ST80 = string[80];
  91.   ST5 = string[5];
  92.  
  93. Const
  94.   EMM_INT                   = $67;
  95.   DOS_Int                   = $21;
  96.   GET_PAGE_FRAME            = $41;
  97.   GET_UNALLOCATED_PAGE_COUNT= $42;
  98.   ALLOCATE_PAGES            = $43;
  99.   MAP_PAGES                 = $44;
  100.   DEALLOCATE_PAGES          = $45;
  101.   GET_VERSION               = $46;
  102.   GETMAPPHYADDARR           = $5800;
  103.   STATUS_OK                 = 0;
  104.  
  105.  
  106. { * --------------------------------------------------------- * }
  107.   { The function Hex_String converts an Word into a four
  108.     character hexadecimal number(string) with leading zeroes.   }
  109.   Function Hex_String(Number: Word): ST5;
  110.     Function Hex_Char(Number: Word): Char;
  111.     Begin
  112.       If Number<10 then
  113.         Hex_Char:=Char(Number+48)
  114.       else
  115.         Hex_Char:=Char(Number+55);
  116.     end; { Function Hex_Char }
  117.  
  118.   Var
  119.     S: ST5;
  120.   Begin
  121.     S:='';
  122.     S:=Hex_Char( (Number shr 1) div 2048);
  123.     Number:=( ((Number shr 1) mod 2048) shl 1)+
  124.             (Number and 1) ;
  125.     S:=S+Hex_Char(Number div 256);
  126.     Number:=Number mod 256;
  127.     S:=S+Hex_Char(Number div 16);
  128.     Number:=Number mod 16;
  129.     S:=S+Hex_Char(Number);
  130.     Hex_String:=S+'h';
  131.   end; { Function Hex_String }
  132.  
  133. { * --------------------------------------------------------- * }
  134.  
  135.   { The function Emm_Installed checks to see if the Expanded
  136.     Memory Manager (EMM) is loaded in memory. It does this by
  137.     looking for the string 'EMMXXXX0', which should be located
  138.     at 10 bytes from the beginning of the code segment pointed
  139.     to by the EMM interrupt, 67h                                }
  140.   Function Emm_Installed: Boolean;
  141.     Var
  142.       Emm_Device_Name       : string[8];
  143.       Int_67_Device_Name    : string[8];
  144.       Position              : Word;
  145.       Regs                  : registers;
  146.  
  147.   Begin
  148.     Int_67_Device_Name:='';
  149.     Emm_Device_Name   :='EMMXXXX0';
  150.     with Regs do
  151.     Begin
  152.       { Get the code segment pointed to by Interrupt 67h, the EMM
  153.       interrupt by using DOS call $35, 'get interrupt vector'     }
  154.       AH:=$35;
  155.       AL:=EMM_INT;
  156.       Intr(DOS_int,Regs);
  157.  
  158.       { The ES pseudo-register contains the segment address pointed
  159.         to by Interrupt 67h }
  160.       { Create an 8 character string from the 8 successive bytes
  161.         pointed to by ES:$0A (10 bytes from ES)                   }
  162.       For Position:=0 to 7 do
  163.         Int_67_Device_Name:=
  164.           Int_67_Device_Name+Chr(mem[ES:Position+$0A]);
  165.       Emm_Installed:=True;
  166.       { Is it the EMM manager signature, 'EMMXXXX0'? then EMM is
  167.         installed and ready for use, if not, then the EMM manager
  168.         is not present                                            }
  169.       If Int_67_Device_Name<>Emm_Device_Name
  170.         then Emm_Installed:=False;
  171.     end; { with Regs do }
  172.   end;  { Function Emm_Installed }
  173.  
  174. { * --------------------------------------------------------- * }
  175.  
  176.   { This function returns the total number of EMS pages present
  177.     in the system, and the number of EMS pages that are
  178.     available for our use                                       }
  179.   Function EMS_Pages_Available
  180.     (Var Total_EMS_Pages,Pages_Available: Word): Word;
  181.   Var
  182.     Regs: Registers;
  183.   Begin
  184.     with Regs do
  185.     Begin
  186.       { Put the desired EMS function number in the AH pseudo-
  187.         register                                                }
  188.       AH:=Get_Unallocated_Page_Count;
  189.       intr(EMM_INT,Regs);
  190.       { The number of EMS pages available is returned in BX     }
  191.       Pages_Available:=BX;
  192.       { The total number of pages present in the system is
  193.         returned in DX                                          }
  194.       Total_EMS_Pages:=DX;
  195.       { Return the error code                                   }
  196.       EMS_Pages_Available:=AH
  197.     end;
  198.   end; { EMS_Pages_Available }
  199.  
  200. { * --------------------------------------------------------- * }
  201.  
  202.   { This function requests the desired number of pages from the
  203.     EMM                                                         }
  204.   Function Allocate_Expanded_Memory_Pages
  205.     (Pages_Needed: Word; Var Handle: Word   ): Word;
  206.   Var
  207.     Regs: Registers;
  208.   Begin
  209.     with Regs do
  210.     Begin
  211.       { Put the desired EMS function number in the AH pseudo-
  212.         register                                                }
  213.       AH:= Allocate_Pages;
  214.       { Put the desired number of pages in BX                   }
  215.       BX:=Pages_Needed;
  216.       intr(EMM_INT,Regs);
  217.       { Our EMS handle is returned in DX                        }
  218.       Handle:=DX;
  219.       { Return the error code }
  220.       Allocate_Expanded_Memory_Pages:=AH;
  221.     end;
  222.   end; { Function Allocate_Expanded_Memory_Pages }
  223.  
  224. { * --------------------------------------------------------- * }
  225.  
  226.   { This function maps a logical page onto one of the physical
  227.     pages made available to us by the
  228.     Allo